//
//  CaseManage_ViewController.m
//  International_Hospital
//
//  Created by LiYiLin on 16/7/4.
//  Copyright © 2016年 LiYiLin. All rights reserved.
//

#import "CaseManage_ViewController.h"
#import "CaseManageTopView.h"
#import "AddCaseController.h"
#import "CaseCell.h"
#import "CaseModel.h"

@interface CaseManage_ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) UITableView * tableView;
@property (nonatomic,strong) NSMutableArray * dataArray;

@end

@implementation CaseManage_ViewController




- (void)viewDidLoad {
    [super viewDidLoad];
    [self settopViewUI];
    [self setupTableViewUI];
    [self setupButton];
    [self.navigationItem.rightBarButtonItem setTintColor:[UIColor whiteColor]];
}


#pragma mark
#pragma mark - 设置个人信息
- (void)settopViewUI{
//    if (!self.dataDic) {
//        return;
//    }

    CaseManageTopView * topView = [[CaseManageTopView alloc]init];
    [self.view addSubview:topView];
    
//    topView.dataDic = self.dataDic;

    [topView makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(64);
        make.left.right.equalTo(0);
        make.height.equalTo(150);
    }];
}

- (void)setupTableViewUI{
    [self.view addSubview:self.tableView];
//    self.tableView. 
    [self.tableView registerClass:[CaseCell class] forCellReuseIdentifier:@"casecell"];
}

#pragma mark
#pragma mark - 设置添加按钮
- (void)setupButton{
    UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:@"添加病例" forState:UIControlStateNormal];
    [button setBackgroundColor:kThemeColor];
    [self.view addSubview:button];
    [button makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(20);
        make.right.equalTo(-20);
        make.bottom.equalTo(-15);
        make.height.equalTo(45);
    }];
    [button addTarget:self action:@selector(clickAddCase) forControlEvents:UIControlEventTouchUpInside];
}


#pragma mark
#pragma mark - 监听点击添加病例方法
- (void)clickAddCase{
    AddCaseController * acc = [AddCaseController new];
    
   
    acc.MyBlock = ^(CaseModel*model){
        
        [self.dataArray addObject:model];
        
        [NSKeyedArchiver archiveRootObject:self.dataArray toFile:[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0] stringByAppendingPathComponent:@"qqq.data"]];
        
        NSIndexPath * indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
        [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
               [self scrollToTop];
    };
    [self.navigationController pushViewController:acc animated:YES];
}

- (void)scrollToTop{

    NSIndexPath * indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    CaseCell * cell = [tableView dequeueReusableCellWithIdentifier:@"casecell" forIndexPath:indexPath];
    
    NSInteger num = self.dataArray.count - indexPath.row -1;
    cell.model = self.dataArray[num];
   return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
     
        [self.dataArray removeObjectAtIndex:self.dataArray.count - indexPath.row -1];
        
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
        
        [NSKeyedArchiver archiveRootObject:self.dataArray toFile:[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0] stringByAppendingPathComponent:@"qqq.data"]];
    }
}

#pragma mark
#pragma mark - 懒加载
- (NSMutableArray*)dataArray{
    if (nil == _dataArray) {
        _dataArray = [NSKeyedUnarchiver unarchiveObjectWithFile:[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0] stringByAppendingPathComponent:@"qqq.data"]];
    if (nil == _dataArray) {
             _dataArray = [NSMutableArray array];
        }
    }
    return _dataArray;
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 220, self.view.bounds.size.width, self.view.bounds.size.height-220-65) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = 130;
    }
    return _tableView;
}




@end

























